package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.common.exception.SsyxException;
import com.atguigu.ssyx.common.jwt.JwtHelper;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.result.ResultCodeEnum;
import com.atguigu.ssyx.common.util.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: IndexController
 * Package: com.atguigu.ssyx.acl.controller
 * Description:
 *
 * @Author: 比特张
 * @Create: 2023/7/13 - 0:50
 * @Version: v1.0
 */
@Api(tags = "登录退出接口")
@RestController
@RequestMapping("/admin/acl/index")
public class IndexController {

    @Resource
    private AdminService adminService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        Admin admin = adminService.getByUsername(loginVo.getUsername());
        if (null == admin) {
            throw new SsyxException(ResultCodeEnum.LOGIN_FAIL);
        }
        if (!MD5.encrypt(loginVo.getPassword()).equals(admin.getPassword())) {
            throw new SsyxException(ResultCodeEnum.LOGIN_FAIL);
        }
        String token = JwtHelper.createToken(admin.getId(), admin.getUsername());

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        return Result.ok(map);
    }

    @ApiOperation("获取信息")
    @GetMapping("/info")
    public Result info() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "atguigu");
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return Result.ok(map);
    }

    @ApiOperation("退出")
    @PostMapping("/logout")
    public Result logout() {
        return Result.ok();
    }
}
